<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $bounty_minturns, $bounty_ratio;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_scan_title']);

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$ship_id = (isset($_GET['ship_id'])) ? $_GET['ship_id'] : '';

// Get opposing ship information
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE ship_id = {int:ship_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'ship_id' => $ship_id
	)
);
$targetinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$playerscore = gen_score($playerinfo['ship_id']);
$targetscore = gen_score($targetinfo['ship_id']);

$playerscore = $playerscore * $playerscore;
$targetscore = $targetscore * $targetscore;

srand((double)microtime()*1000000);

// Check to ensure target is in the same sector as player.
if ($targetinfo['sector'] != $playerinfo['sector'])
	echo $txt['bnt_planet_noscan'];
else
{
	if ($playerinfo['turns'] < 1)
		echo $txt['bnt_scan_turn'];
	else
	{
		// Determine percent chance of success in scanning target ship - based on player's sensors and opponent's cloak.
		$success= SCAN_SUCCESS($playerinfo['sensors'], $targetinfo['cloak']);
		if ($success < 5)
			$success = 5;
		if($success > 95)
			$success = 95;
			
		$roll = rand(1, 100);
		if ($roll > $success)
		{
			// If scan fails - inform both player and target.
			echo $txt['bnt_planet_noscan'];
			playerlog($targetinfo['ship_id'], LOG_SHIP_SCAN_FAIL, "$playerinfo[character_name]");
		}
		else
		{
			// If scan succeeds, show results and inform target.
			// Scramble the results by scan error factor.

			// Get total bounty on this player, if any.
			$btyamount = 0;
			$hasbounty = $smcFunc['db_query']('', '
				SELECT SUM(amount) AS btytotal
				FROM {raw:db_bounty}
				WHERE bounty_on = {int:ship_id}',
				array(
					'db_bounty' => $dbtables['bounty'],
					'ship_id' =>  $targetinfo['ship_id']
				)
			);
			
			if ($smcFunc['db_num_rows']($hasbounty) > 0)
			{
				$resx = $smcFunc['db_fetch_assoc']($hasbounty);
				$smcFunc['db_free_result']($hasbounty);
				
				if ($resx['btytotal'] > 0) 
				{
					$btyamount = NUMBER($resx['btytotal']);
					
					$txt['bnt_scan_bounty'] = str_replace("[amount]", $btyamount, $txt['bnt_scan_bounty']);
					echo $txt['bnt_scan_bounty'], '<br>';
					
					$btyamount = 0;
					
					// Check for Federation bounty
					$hasfedbounty = $smcFunc['db_query']('', '
						SELECT SUM(amount) AS btytotal
						FROM {raw:db_bounty}
						WHERE bounty_on = {int:ship_id}
						AND placed_by = 0',
						array(
							'db_bounty' => $dbtables['bounty'],
							'ship_id' =>  $targetinfo['ship_id']
						)
					);
					
					if ($smcFunc['db_num_rows']($hasfedbounty) > 0)
					{
						$resy = $smcFunc['db_fetch_assoc']($hasfedbounty);
						$smcFunc['db_free_result']($hasfedbounty);
						
						if ($resy['btytotal'] > 0) 
						{ 
							$btyamount = $resy['btytotal'];
							echo $txt['bnt_scan_fedbounty'], '<br>';
						}
					}
				}
			}
			
			// Player will get a Federation Bounty on themselves if they attack a player who's score is less than bounty_ratio of
			// themselves. If the target has a Federation Bounty, they can attack without attracting a bounty on themselves.
			if ($targetinfo['xenobe'] == "N" && $btyamount == 0 && ((($targetscore / $playerscore) < $bounty_ratio) || $targetinfo['turns_used'] < $bounty_minturns))
				echo $txt['bnt_by_fedbounty'], '<br><br>';
			else
				echo $txt['bnt_by_nofedbounty'], '<br><br>';
			
			$sc_error = SCAN_ERROR($playerinfo['sensors'], $targetinfo['cloak']);
			echo '
					', $txt['bnt_scan_ron'], ' ', $targetinfo['ship_name'], ', ', $txt['bnt_scan_capt'], ' ', $targetinfo['character_name'], '<br><br>
					<b>', $txt['bnt_ship_levels'], ':</b><br><br>
					<table width="" border="0" cellspacing="0" cellpadding="4">
						<tr>
							<td>', $txt['bnt_hull'], ':</td>';
			
			// Determine and display results of hull scan.
			$roll = rand(1,100);	
			if ($roll < $success)
			{
				$sc_hull = round($targetinfo['hull'] * $sc_error / 100);
				echo '		<td>', $sc_hull, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
			
			// Determine and display results of engine scan.
			echo '
						<tr>
							<td>', $txt['bnt_engines'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_engines = round($targetinfo['engines'] * $sc_error / 100);
				echo '		<td>', $sc_engines, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of power scan.
			echo '
						<tr>
							<td>', $txt['bnt_power'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_power = round($targetinfo['power'] * $sc_error / 100);
				echo '		<td>', $sc_power, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of computer scan.
			echo '
						<tr>
							<td>', $txt['bnt_computer'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_computer = round($targetinfo['computer'] * $sc_error / 100);
				echo '		<td>', $sc_computer, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of sensors scan.
			echo '
						<tr>
							<td>', $txt['bnt_sensors'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_sensors = round($targetinfo['sensors'] * $sc_error / 100);
				echo '		<td>', $sc_sensors, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of beams scan.
			echo '
						<tr>
							<td>', $txt['bnt_beams'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_beams = round($targetinfo['beams'] * $sc_error / 100);
				echo '		<td>', $sc_beams, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of torpedo launcher scan.
			echo '
						<tr>
							<td>', $txt['bnt_torp_launch'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_torp_launchers = round($targetinfo['torp_launchers'] * $sc_error / 100);
				echo '		<td>', $sc_torp_launchers, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of armor scan.
			echo '
						<tr>
							<td>', $txt['bnt_armor'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_armor = round($targetinfo['armor'] * $sc_error / 100);
				echo '		<td>', $sc_armor, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of shields scan.
			echo '
						<tr>
							<td>', $txt['bnt_shields'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_shields = round($targetinfo['shields'] * $sc_error / 100);
				echo '		<td>', $sc_shields, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of cloak scan.
			echo '
						<tr>
							<td>', $txt['bnt_cloak'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_cloak = round($targetinfo['cloak'] * $sc_error / 100);
				echo '		<td>', $sc_cloak, '</td>
						</tr>', chr(10);
			}
			else
			{
				echo '		<td>???</td>
						</tr>', chr(10);
			}
	  
			// Determine and display results of armor points scan.
			echo '
					</table><br>
					<b>', $txt['bnt_scan_arma'], '</b><br><br>
					<table width="" border="0" cellspacing="0" cellpadding="4">
						<tr>
							<td>', $txt['bnt_armorpts'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_armor_pts = round($targetinfo['armor_pts'] * $sc_error / 100);
				echo '		<td>', $sc_armor_pts, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of ship fighers scan.
			echo '
						<tr>
							<td>', $txt['bnt_fighters'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_fighters = round($targetinfo['ship_fighters'] * $sc_error / 100);
				echo '		<td>', $sc_ship_fighters, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of ship torpedoes scan.
			echo '
						<tr>
							<td>', $txt['bnt_torps'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_torps = round($targetinfo['torps'] * $sc_error / 100);
				echo '		<td>', $sc_torps, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of ship credits scan.
			echo '
					</table><br>
					<b>', $txt['bnt_scan_carry'], '</b><br><br>
					<table width="" border="0" cellspacing="0" cellpadding="4">
						<tr>
							<td>', $txt['bnt_credits'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_credits = round($targetinfo['credits'] * $sc_error / 100);
				echo '		<td>', $sc_credits, '</td>
						</tr>', chr(10);
			}
			else
			{
				echo '		<td>???</td>
						</tr>', chr(10);
			}

			// Determine and display results of ship colonists scan.
			echo '
						<tr>
							<td>', $txt['bnt_colonists'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_colonists = round($targetinfo['ship_colonists'] * $sc_error / 100);
				echo '		<td>', $sc_ship_colonists, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship energy scan.
			echo '
						<tr>
							<td>', $txt['bnt_energy'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_energy = round($targetinfo['ship_energy'] * $sc_error / 100);
				echo '		<td>', $sc_ship_energy, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship ore scan.
			echo '
						<tr>
							<td>', $txt['bnt_ore'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_ore = round($targetinfo['ship_ore'] * $sc_error / 100);
				echo '		<td>', $sc_ship_ore, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship organics scan.
			echo '
						<tr>
							<td>', $txt['bnt_organics'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_organics = round($targetinfo['ship_organics'] * $sc_error / 100);
				echo '		<td>', $sc_ship_organics, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship goods scan.
			echo '
						<tr>
							<td>', $txt['bnt_goods'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_ship_goods = round($targetinfo['ship_goods'] * $sc_error / 100);
				echo '		<td>', $sc_ship_goods, '</td>
						</tr>', chr(10);
			}
			else
			{
				echo '		<td>???</td>
						</tr>', chr(10);
			}

			// Determine and display results of ship warp editors scan.
			echo '
					</table><br>
					<b>', $txt['bnt_devices'], ':</b><br><br>
					<table width="" border="0" cellspacing="0" cellpadding="4">
						<tr>
							<td>', $txt['bnt_warpedit'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_dev_warpedit = round($targetinfo['dev_warpedit'] * $sc_error / 100);
				echo '		<td>', $sc_dev_warpedit, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of ship genesis torpedoes scan.
			echo '
						<tr>
							<td>', $txt['bnt_genesis'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_dev_genesis = round($targetinfo['dev_genesis'] * $sc_error / 100);
				echo '		<td>', $sc_dev_genesis, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}

			// Determine and display results of ship mines deflectors can.
			echo '
						<tr>
							<td>', $txt['bnt_deflect'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_dev_minedeflector = round($targetinfo['dev_minedeflector'] * $sc_error / 100);
				echo '		<td>', $sc_dev_minedeflector, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship emergy warp devices can.
			echo '
						<tr>
							<td>', $txt['bnt_ewd'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				$sc_dev_emerwarp = round($targetinfo['dev_emerwarp'] * $sc_error / 100);
				echo '		<td>', $sc_dev_emerwarp, '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
	  
			// Determine and display results of ship escape pod can.
			echo '
						<tr>
							<td>', $txt['bnt_escape_pod'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				echo '		<td>', $targetinfo['dev_escapepod'], '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
		
			// Determine and display results of ship fuel scoop can.
			echo '
						<tr>
							<td>', $txt['bnt_fuel_scoop'], ':</td>';
			$roll = rand(1,100);
			if ($roll < $success)
			{
				echo '		<td>', $targetinfo['dev_fuelscoop'], '</td>
						</tr>';
			}
			else
			{
				echo '		<td>???</td>
						</tr>';
			}
			
			echo '
						</table>';
			
			playerlog($targetinfo['ship_id'], LOG_SHIP_SCAN, "$playerinfo[character_name]");
		}
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET turns = turns - 1,
				turns_used = turns_used + 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
	}
}

echo '<br><br>';
TextMainMenu();
?>
